import com.powerfl.frm.taf.*;

import flash.display.*;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.*;


final public function error( $msg:String ):void{
	throw new Error( $msg );
}
final public function log( $msg:String ):void{
	var temp:*;
	temp = STAGE.getChildByName( '@@log' );
	if( temp ){
		STAGE.setChildIndex( temp, STAGE.numChildren - 1 );
	}else{
		temp = new TextField;
		TextField(temp).multiline = true;
		TextField(temp).defaultTextFormat = new TextFormat( 'Arial', 12, 0xffffff );
		temp.name = '@@log';
		temp.width = STAGE.stageWidth;
		temp.height = STAGE.stageHeight;
		STAGE.addChild( temp );
	}
	TextField(temp).htmlText += $msg + '<Br>';
	TextField(temp).scrollV = TextField(temp).maxScrollV;
	
}
final public function BREAK( ...$param ):void{
	var GLOBAL:*, EACHS:*, PARAM:*;
	GLOBAL = global;
	PARAM = $param;
	throw new Error( $param );
}

final public function typeofObject( $target:* ):Boolean{
	return $target.toString() === '[object Object]';
}
//-----------------------------------------------------------------------------------------------------------
final public function apply( $func:Function, $param:Array, $startIdx:int = -1, $endIdx:int = -1 ):*{
	if( $startIdx > -1 ){
		if( $endIdx < 0 ){
			$endIdx = $param.length;
		}
		return $func.apply( null, $param.slice( $startIdx, $endIdx + 1 ) );
	}else if( $endIdx > -1 ){
		if( $startIdx < 0 ){
			$startIdx = 0;
		}
		return $func.apply( null, $param.slice( $startIdx, $endIdx + 1 ) );
	}else{
		return $func.apply( null, $param );
	}
}
//-----------------------------------------------------------------------------------------------------------
final public function eachKey( $trees:*, $flow:*, $param:Array = null, ...$ignoreKeys ):void{
	orderBranchAdd( function():void{
		var key:*, data:*, flow:Function, temp:TAFeach;
		if( $trees is String ){
			data = treeGet( $trees );
		}else{
			data = $trees; 
		}
		if( $flow is String ){
			flow = flowGet( $flow );
		}else{
			flow = $flow;
		}
		temp = tdata( '@eachs' )[$flow] = poolPullEach();
		for( key in data ){
			if( $ignoreKeys.indexOf( key ) === -1 ){
				orderAdd( null, function( $key:*, $data:*, $container:* ):void{
					temp.key = $key;
					temp.data = $data;
					temp.container = $container;
					if( $param ){
						flow.apply( null, $param );
					}else{
						flow();
					}
					++temp.serial;
				}, key, data[key], data );
			}
		}
		orderAdd( null, function():void{
			poolCollectEach( temp );
			tdata( '@eachs' )[$flow] = null;
		} );
	} );
}
final public function eachSerial( $trees:*, $flow:*, $param:Array = null, ...$ignoreKeys ):void{
	orderBranchAdd( function():void{
		var i:int, j:int, data:*, flow:Function, temp:TAFeach;
		flow = flowGet( $flow );
		temp = tdata( '@eachs' )[$flow] = poolPullEach();
		if( $trees is Number ){
			data = [];
			j = $trees; 
		}else if( $trees is String ){
			data = treeGet( $trees );
			j = data.length;
		}else if( $trees ){
			data = $trees;
			j = data.length;
		}
		for( i = 0 ; i < j ; ++i ){
			if( $ignoreKeys.indexOf( i ) === -1 ){
				orderAdd( null, function( $each:TAFeach, $key:*, $data:*, $container:* ):void{
					$each.key = $key;
					$each.data = $data;
					$each.container = $container;
					if( $param ){
						flow.apply( null, $param );
					}else{
						flow();
					}
				}, temp, i, data[i], data );
			}
		}
		orderAdd( null, function():void{
			poolCollectEach( temp );
			tdata( '@eachs' )[$flow] = null;
		} );
	} );
}